package cn.edu.huanu.dao.impl;

import cn.edu.huanu.dao.AddressDao;
import cn.edu.huanu.exception.DaoException;
import cn.edu.huanu.pojo.Address;
import cn.edu.huanu.pojo.User;
import cn.edu.huanu.utils.ConnectionUtils;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hb
 * @date 2025-11-20 12:40:06
 */
public class AddressDaoImpl implements AddressDao {
    @Override
    public List<Address> queryAddressByUid(Integer uid) throws DaoException {
        String sql = """
                select a.id as aid,consignee,consigneetel,address,isdefault,uid,username,email 
                from t_address a,t_user t
                where a.uid = t.id and a.uid =?;
                """;
        return ConnectionUtils.query(sql,rs->{
            List<Address> lists =new ArrayList<>();
            while(rs.next()){
               Address address =   parseAddress(rs);
               lists.add(address);
            }
            return lists;
        },uid);

    }

    private Address parseAddress(ResultSet rs) throws Exception{
         Address address =new Address();

         address.setId(rs.getInt("aid"));
         address.setConsignee(rs.getString("consignee"));
         address.setTelphone(rs.getString("consigneetel"));
         address.setAddress(rs.getString("address"));
         address.setIsDefault(rs.getInt("isdefault"));
        User u=new User();
        u.setId(rs.getInt("uid"));
        u.setUsername(rs.getString("username"));
        u.setEmail(rs.getString("email"));

        address.setUser(u);


         return address;
    }
}
